package solver.moves;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import util.ObjectIdPropertyPair;

import model.IncrementalCostCalculator;
import model.IncrementalValidator;
import model.MachineSelector;
import model.SolverEnvironment;
import model.Problem;
import model.Solution;

import command.Invoker;

public class SingleProcessOfMostLoadedMachineToBestMachineMove extends
		AbstractMove {

	MachineSelector ms;
	
	public SingleProcessOfMostLoadedMachineToBestMachineMove(SolverEnvironment env, MachineSelector ms) {
		super(env);
		this.ms=ms;
	}

	@Override
	public boolean performMove() {
		int worstMachine = ms.selectMostLoadedMachine(getRandom().nextInt(Math.min(Math.max(getProblem().M/10,1),20))+1);
		List<Integer> mp = env.getPotentialSolution().getProcessesForMachine(worstMachine);
		if(mp.size()==0)
			return false;
		int pId = mp.get(getRandom().nextInt(mp.size()));
		int start = worstMachine+1;
		int end =  start+ getProblem().M-1;
		int newm = testSequentiallyBestNoSchedule(pId,worstMachine, start,end);
		if(newm == -1 || newm == worstMachine){
			return false;
		}
		else{
			env.getCostCalculator().getProcessMoveToMachineCost(env.getPotentialSolution(),
					pId, worstMachine, newm);
			makeMove(pId, worstMachine, newm);
			return true;
		}
	}

}
